home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / crc.swg / 0010_Quick CRC Methods.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-27  |  1.4 KB  |  68 lines

  1. {
  2. SEAN PALMER
  3.  
  4. Here are some that make their tables on the fly (to save echo space)
  5.  
  6. I believe crc should be inited to 0 at start
  7.  
  8. This CRC-16 is not identical to the one used by the Xmodem and Zmodem
  9. File transfer protocols. The polynomial is the same
  10. (X^16+X^12+X^5+X^0 or 0x8408) but the bit-ordering is the opposite,
  11. and preconditioning and postconditioning is used as in 32-bit CRCs.
  12. This method is also used by the European version of X.25.
  13. }
  14.  
  15. Var
  16.   crc16table : Array [Byte] of Word;
  17.  
  18. Procedure makeCRC16table;
  19. Var
  20.   crc : Word;
  21.   i,n : Byte;
  22. begin
  23.   For i := 0 to 255 do
  24.   begin
  25.     crc := i;
  26.     For n := 1 to 8 do
  27.       if odd(crc) then
  28.         crc := (crc shr 1) xor $8408
  29.       else
  30.         crc := crc shr 1;
  31.  
  32.     crc16table[i] := crc;
  33.   end;
  34. end;
  35.  
  36. Function updateCRC16(c : Byte; crc : Word) : Word;
  37. begin
  38.   updateCRC16 := crc16table[lo(crc) xor c] xor hi(crc);
  39. end;
  40.  
  41. {this is the same crc used For zModem crc32}
  42.  
  43. Var
  44.   crc32table : Array [Byte] of LongInt;
  45.  
  46. Procedure makeCRC32table;
  47. Var
  48.   crc : LongInt;
  49.   i,n : Byte;
  50. begin
  51.   For i := 0 to 255 do
  52.   begin
  53.     crc := i;
  54.     For n := 1 to 8 do
  55.       if odd(crc) then
  56.         crc := (crc shr 1) xor $EDB88320
  57.       else
  58.         crc := crc shr 1;
  59.  
  60.     crc32table[i] := crc;
  61.   end;
  62. end;
  63.  
  64. Function updateCRC32(c : Byte; crc : LongInt) : LongInt;
  65. begin
  66.   updateCRC32 := crc32table[lo(crc) xor c] xor (crc shr 8);
  67. end;
  68.